﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class public_html_NewSite_CMSV2_GamesScreenshot : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        lblTitel.Text = Request.QueryString["titel"];
        int i = 0;
        DivCover.InnerHtml = "<table border=\"0\"><tr>";
        DivScreens.InnerHtml = "<table border=\"0\"><tr>";

        OdbcDataReader reader = SQL.ExecuteReader("SELECT * FROM V2_Screenshots WHERE SpilID = '" + Request.QueryString["id"] + "' GROUP BY Filnavn ORDER BY Format");
        while (reader.Read())
        {
            if (i == 5)
            {
                i = 0;
                DivScreens.InnerHtml += "</tr><tr>";
            }
            DivScreens.InnerHtml += "<td><img width=\"130px\" src=\"../screenshots/" + reader["Filnavn"].ToString() + "\"><br><center><a href=\"GamesScreenshotDelete.aspx?id=" + reader["id"].ToString() + "\">Slet</a></center></td>";
            i++;
        }

        int y = 0;

        while(y != 7)
        {
            if (File.Exists(Server.MapPath("../covers/" + Request.QueryString["id"] + "_" + y.ToString() + ".jpg")))
            {
                DivCover.InnerHtml += "<td><img width=\"80px\" src=\"../covers/" + Request.QueryString["id"] + "_" + y.ToString() + ".jpg\"></td>";
            }
            y++;
        }

        DivCover.InnerHtml += "</tr></table>";
        DivScreens.InnerHtml += "</tr></table>";
    }

    protected void BtmUpload_Click(object sender, EventArgs e)
    {
        string Ext = "";
        string NewFileName = "";
        string PathSave = Server.MapPath("../screenshots/");
        if (FileUpload1.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        }
        if (FileUpload2.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload2.PostedFile.FileName);
            FileUpload2.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        }
        if (FileUpload3.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload3.PostedFile.FileName);
            FileUpload3.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        }
        if (FileUpload4.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload4.PostedFile.FileName);
            FileUpload4.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        }
        if (FileUpload5.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload5.PostedFile.FileName);
            FileUpload5.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        } 
        if (FileUpload6.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload6.PostedFile.FileName);
            FileUpload6.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        } 
        if (FileUpload7.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload7.PostedFile.FileName);
            FileUpload7.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        }
        if (FileUpload8.HasFile)
        {
            NewFileName = Guid.NewGuid().ToString();
            Ext = Path.GetExtension(FileUpload8.PostedFile.FileName);
            FileUpload8.SaveAs(@PathSave + NewFileName + Ext);
            SaveToDB(NewFileName + Ext);
        }
        lblUploadStatus.Text = "Screenshotsne blev uploadet";
    }

    public string SaveToDB(string FileName)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        for (int i = 0; i < ListFormat.Items.Count; i++)
        {
            if (ListFormat.Items[i].Selected)
            {
                SQL.ExecuteNonQuery("INSERT INTO V2_Screenshots(SpilID, Format, Filnavn) VALUES('" + Request.QueryString["id"] + "','" + ListFormat.Items[i].Value + "','" + FileName + "')");
            }
        }

        return "OK";
    }
    protected void UploadCover_Click(object sender, EventArgs e)
    {
        string PathSave = Server.MapPath("../covers/");
        string FileName = Request.QueryString["id"];

        if(FileUploadXbox360.HasFile)
        {
            FileUploadXbox360.SaveAs(@PathSave + FileName + "_1.jpg");
        }
        if (FileUploadPC.HasFile)
        {
            FileUploadPC.SaveAs(@PathSave + FileName + "_2.jpg");
        }
        if (FileUploadPlaystation3.HasFile)
        {
            FileUploadPlaystation3.SaveAs(@PathSave + FileName + "_3.jpg");
        }
        if (FileUploadWii.HasFile)
        {
            FileUploadWii.SaveAs(@PathSave + FileName + "_4.jpg");
        }

        lblCover.Text = "Coverne blev uploadet";
    }
}
